home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / apps / ILBMtoC / ILBMtoC.c
Encoding:
C/C++ Source or Header  |  1998-04-26  |  4.2 KB  |  158 lines

  1. /*--------------------------------------------------------------*/
  2. /*                                */
  3. /* ILBMtoC: reads in ILBM, prints out ascii representation,     */
  4. /*  for including in C files.                     */
  5. /*                                                              */
  6. /* Based on ILBMDump.c by Jerry Morrison and Steve Shaw,    */
  7. /* Electronic Arts.                               */
  8. /* Jan 31, 1986                            */
  9. /*                                                              */
  10. /* This software is in the public domain.                       */
  11. /* This version for the Amiga computer.                         */
  12. /*                                                              */
  13. /*  Callable from CLI ONLY                    */
  14. /*  modified 05-91 for use wuth iffparse modules        */
  15. /*  Requires linkage with several other modules - see Makefile  */
  16. /*--------------------------------------------------------------*/
  17.  
  18. #include "iffp/ilbmapp.h"
  19.  
  20. #ifdef LATTICE
  21. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  22. int chkabort(void) { return(0); }  /* really */
  23. #endif
  24.  
  25. char *vers = "\0$VER: ILBMtoC 37.5";
  26. char *Copyright = "ILBMtoC v37.5 (Freely Redistributable)";
  27.  
  28. void GetSuffix(UBYTE *to, UBYTE *fr);
  29. void bye(UBYTE *s, int e);
  30. void cleanup(void);
  31.  
  32. struct Library *IFFParseBase = NULL;
  33. struct Library *GfxBase = NULL;
  34.  
  35. /* ILBM frame */
  36. struct ILBMInfo ilbm = {0};
  37.  
  38.  
  39. /* ILBM Property chunks to be grabbed - only BMHD needed for this app
  40.  */
  41. LONG    ilbmprops[] = {
  42.         ID_ILBM, ID_BMHD,
  43.         TAG_DONE
  44.         };
  45.  
  46. /* ILBM Collection chunks (more than one in file) to be gathered */
  47. LONG    *ilbmcollects = NULL;    /* none needed for this app */
  48.  
  49. /* ILBM Chunk to stop on */
  50. LONG    ilbmstops[] = {
  51.         ID_ILBM, ID_BODY,
  52.         TAG_DONE
  53.         };
  54.  
  55.  
  56. UBYTE defSwitch[] = "b";
  57.  
  58. /** main() ******************************************************************/
  59.  
  60. void main(int argc, char **argv)
  61.     {
  62.     UBYTE *sw;
  63.     FILE *fp;
  64.     LONG error=NULL;
  65.     UBYTE *ilbmname, name[80], fname[80];
  66.  
  67.     if ((argc < 2)||(argv[argc-1][0]=='?'))
  68.     {
  69.     printf("Usage from CLI: 'ILBMtoC filename switch-string'\n");
  70.     printf(" where switch-string = \n");
  71.     printf("  <nothing> : Bob format (default)\n");
  72.     printf("  s         : Sprite format (with header and trailer words)\n");
  73.     printf("  sn        : Sprite format (No header and trailer words)\n");
  74.     printf("  a         : Attached sprite (with header and trailer)\n");
  75.     printf("  an        : Attached sprite (No header and trailer)\n");
  76.     printf(" Add 'c' to switch list to output CR's with LF's   \n");
  77.     exit(RETURN_OK);
  78.     }
  79.     
  80.  
  81.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  82.     bye("Can't open graphics.library",RETURN_FAIL);
  83.  
  84.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  85.     bye("Can't open iffparse.library",RETURN_FAIL);
  86.  
  87. /*
  88.  * Here we set up default ILBMInfo fields for our
  89.  * application's frames.
  90.  * Above we have defined the propery and collection chunks
  91.  * we are interested in (some required like BMHD)
  92.  */
  93.     ilbm.ParseInfo.propchks      = ilbmprops;
  94.     ilbm.ParseInfo.collectchks   = ilbmcollects;
  95.     ilbm.ParseInfo.stopchks      = ilbmstops;
  96.     if(!(ilbm.ParseInfo.iff = AllocIFF()))
  97.         bye(IFFerr(IFFERR_NOMEM),RETURN_FAIL);    /* Alloc an IFFHandle */
  98.  
  99.     sw = (argc>2) ? (UBYTE *)argv[2] : defSwitch;
  100.     ilbmname = argv[1];
  101.  
  102.     if (error = loadbrush(&ilbm,ilbmname))
  103.         {
  104.         printf("Can't load ilbm \"%s\", ifferr=%s\n",ilbmname,IFFerr(error));
  105.         bye("",RETURN_WARN);
  106.         }
  107.     else /* Successfully loaded ILBM */
  108.     {
  109.     printf(" Creating file %s.c \n",argv[1]);
  110.     GetSuffix(name,argv[1]);
  111.     strcpy(fname,argv[1]);
  112.     strcat(fname,".c");
  113.     fp = fopen(fname,"w");
  114.     if(fp)
  115.         {
  116.         BMPrintCRep(ilbm.brbitmap,fp,name,sw);
  117.         fclose(fp);
  118.         }
  119.     else  printf("Couldn't open output file: %s. \n", fname);
  120.     unloadbrush(&ilbm);
  121.     }
  122.     printf("\n");
  123.     bye("",RETURN_OK);
  124.     }
  125.  
  126.  
  127.  
  128. /* this copies part of string after the last '/' or ':' */
  129. void GetSuffix(to, fr) UBYTE *to, *fr; {
  130.     int i;
  131.     UBYTE c,*s = fr;
  132.     for (i=0; ;i++) {
  133.     c = *s++;
  134.     if (c == 0) break;
  135.     if (c == '/') fr = s;
  136.     else if (c == ':') fr = s;
  137.     }
  138.     strcpy(to,fr);
  139.     }
  140.  
  141.  
  142. void bye(UBYTE *s, int e)
  143.     {
  144.     if(s&&(*s))    printf("%s\n",s);
  145.     cleanup();
  146.     exit(e);
  147.     }
  148.  
  149.  
  150. void cleanup()
  151.     {
  152.     if(ilbm.ParseInfo.iff)        FreeIFF(ilbm.ParseInfo.iff);
  153.  
  154.     if(IFFParseBase)    CloseLibrary(IFFParseBase);
  155.     if(GfxBase)        CloseLibrary(GfxBase);
  156.     }
  157.  
  158.